package cn.iocoder.yudao.module.vote.controller.admin.voteItem;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vote.controller.admin.voteItem.vo.*;
import cn.iocoder.yudao.module.vote.dal.dataobject.voteItem.VoteItemDO;
import cn.iocoder.yudao.module.vote.service.voteItem.VoteItemService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 投票行")
@RestController
@RequestMapping("/vote/item")
@Validated
public class VoteItemController {

    @Resource
    private VoteItemService itemService;

    @PostMapping("/create")
    @Operation(summary = "创建投票行")
    @PreAuthorize("@ss.hasPermission('vote:item:create')")
    public CommonResult<Long> createItem(@Valid @RequestBody VoteItemSaveReqVO createReqVO) {
        return success(itemService.createItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新投票行")
    @PreAuthorize("@ss.hasPermission('vote:item:update')")
    public CommonResult<Boolean> updateItem(@Valid @RequestBody VoteItemSaveReqVO updateReqVO) {
        itemService.updateItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除投票行")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vote:item:delete')")
    public CommonResult<Boolean> deleteItem(@RequestParam("id") Long id) {
        itemService.deleteItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得投票行")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vote:item:query')")
    public CommonResult<VoteItemRespVO> getItem(@RequestParam("id") Long id) {
        VoteItemDO item = itemService.getItem(id);
        return success(BeanUtils.toBean(item, VoteItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得投票行分页")
    @PreAuthorize("@ss.hasPermission('vote:item:query')")
    public CommonResult<PageResult<VoteItemRespVO>> getItemPage(@Valid VoteItemPageReqVO pageReqVO) {
        PageResult<VoteItemDO> pageResult = itemService.getItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VoteItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出投票行 Excel")
    @PreAuthorize("@ss.hasPermission('vote:item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportItemExcel(@Valid VoteItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VoteItemDO> list = itemService.getItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "投票行.xls", "数据", VoteItemRespVO.class,
                        BeanUtils.toBean(list, VoteItemRespVO.class));
    }

}